<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>

<sec:authentication property="principal" var="currentPrincipal" scope="page" />

<div class="modal-dialog x-modal-dialog" style="width: 600px;">

  <div class="modal-content">

    <div class="modal-header x-breadcrumb clearfix">

      <h2><i class="glyphicon glyphicon-fire" title=""></i>&nbsp;&nbsp;<span>导出课程表</span></h2>

      <button type="button" class="close" data-dismiss="modal">&times;</button>

    </div>

    <form class="form-horizontal" method="GET" action="${ctx.host}/schedules/export" data-validate = "true">
      <input type="hidden" name="view" value="${view}">

      <div class="modal-body">
      
        <c:choose>
          <c:when test="${currentPrincipal.siteServer}">
            <input type="hidden" name="site" value="${currentPrincipal.site.id}">
          </c:when>
          <c:otherwise>
            <div class="form-group x-form-group">
              <label class="col-xs-3 control-label">门店</label>
              <div class="col-xs-7">
                <select name="site" class="form-control select2" data-validate = "required">
                    <c:forEach var="site" items="${persist.getSites(currentPrincipal.id)}">
                      <option value="${site.id}" ${site.id == param.site ? 'selected' : ''}>${site.name}</option>
                    </c:forEach>
                </select>
              </div>
            </div>
          </c:otherwise>
        </c:choose>
        
        <div class="form-group x-form-group">
          <label class="col-xs-3 control-label">开始日期</label>
          <div class="col-xs-7">
            <input type="text" class="form-control date begin-date" name="beginDate" data-validate = "required"/>
          </div>
        </div>
        
        <div class="form-group x-form-group">
          <label class="col-xs-3 control-label">结束日期</label>
          <div class="col-xs-7">
            <input type="text" class="form-control date end-date" name="endDate" data-validate = "required"/>
          </div>
        </div>
        
      </div>

      <div class="modal-footer">
        <button type="submit" class="btn btn-success btn-shadow btn-shadow-success x-btn">确定</button>&nbsp;&nbsp;&nbsp;&nbsp;
        <button type="button" class="btn btn-default btn-shadow btn-shadow-default x-btn" data-dismiss="modal">取消</button>
      </div>

    </form>

  </div>

</div>

<script type="text/javascript">
  $(function() {
    
    $('.date').datetimepicker({
        format: 'yyyy-mm-dd',
        autoclose: true,
        language: 'zh-CN',
        todayBtn: 'linked',
        todayHighlight: true,
        pickerPosition: 'bottom-right',
        minView: 'month'
      });
    
    $('.begin-date').datetimepicker().on('changeDate', function(e){
         var beginDate = $("input[name='beginDate']").val();
         var endDate = getNextMonth(beginDate);
         console.log(endDate);
         $('.end-date').datetimepicker('setStartDate', beginDate);
         $('.end-date').datetimepicker('setEndDate', endDate);
         
    });
    
    function getNextMonth(date) {  
        var arr = date.split('-');  
        var year = arr[0]; //获取当前日期的年份  
        var month = arr[1]; //获取当前日期的月份  
        var day = arr[2]; //获取当前日期的日  
        var days = new Date(year, month, 0);  
        days = days.getDate(); //获取当前日期中的月的天数  
        var year2 = year;  
        var month2 = parseInt(month) + 1;  
        if (month2 == 13) {  
            year2 = parseInt(year2) + 1;  
            month2 = 1;  
        }  
        var day2 = day;  
        var days2 = new Date(year2, month2, 0);  
        days2 = days2.getDate();  
        if (day2 > days2) {  
            day2 = days2;  
        }  
        if (month2 < 10) {  
            month2 = '0' + month2;  
        }  
      
        var t2 = year2 + '-' + month2 + '-' + day2;  
        return t2;  
    } 
    
    
    $('.select2').select2({
      placeholder : '请选择',
      minimumResultsForSearch : -1,
      allowClear : true,
      width: '100%'
     });
  })
</script>
